//
//  ReadyTableViewCell.m
//  51易借
//
//  Created by 张睿 on 2017/9/12.
//  Copyright © 2017年 张睿. All rights reserved.
//

#import "ReadyTableViewCell.h"


@interface ReadyTableViewCell ()

@property (nonatomic, weak)UIView * backView;
@property (nonatomic, strong)UILabel * borrowMoneyLabel;
@property (nonatomic, strong)UILabel * borrowMoneyValueLabel;
@property (nonatomic, strong)UILabel * repayMoneyLabel;
@property (nonatomic, strong)UILabel * repayMoneyValueLabel;
@property (nonatomic, weak)UIImageView * stateImageView;
@property (nonatomic, weak)UIView * lineView;
@property (nonatomic, strong)UILabel * borrowTimeLabel;
@property (nonatomic, strong)UILabel * borrowTimeValueLabel;
@property (nonatomic, strong)UILabel * repayTimeLabel;
@property (nonatomic, strong)UILabel * repayTimeValueLabel;
@property (nonatomic, strong)UILabel * payMoneyTimeLabel;
@property (nonatomic, strong)UILabel * payMoneyTimeValueLabel;
@property (nonatomic, weak)UIView * oneLineView;
@property (nonatomic, weak)UIView * twoLineView;
@property (nonatomic, weak)UIView * threeLineView;
@property (nonatomic, weak)UIView * fourLineView;


@end

@implementation ReadyTableViewCell


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    if ([super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        [self setupUI];
    }
    
    return self;
}


- (void)setupUI{
    
    self.backgroundColor = [UIColor clearColor];
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
    UIView * backView = [UIView new];
    
    backView.backgroundColor = [UIColor whiteColor];
    
    self.backView = backView;
    
    [self.contentView addSubview:self.backView];
    
    [self.backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.contentView).offset(6 * SCREENH6);
        make.top.mas_equalTo(self.contentView).offset(8 * SCREENH6);
        make.trailing.mas_equalTo(self.contentView).offset(-6 * SCREENH6);
        make.bottom.mas_equalTo(self.contentView);
    }];
    
    self.borrowMoneyLabel = [self setupBigLabelWith:@"借款金额:"];
    
    [self.contentView addSubview:self.borrowMoneyLabel];
    
    [self.borrowMoneyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.contentView).offset(26 * SCREENH6);
        make.leading.mas_equalTo(self.contentView).offset(17 * SCREENH6);
    }];
    
    self.borrowMoneyValueLabel = [self setupBigLabelWith:@"￥100.00"];
    
    [self.contentView addSubview:self.borrowMoneyValueLabel];
    
    [self.borrowMoneyValueLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.borrowMoneyLabel);
        make.leading.mas_equalTo(self.borrowMoneyLabel.mas_trailing);
    }];
    
    UIView * oneLineView = [UIView new];
    
    oneLineView.backgroundColor = ColorWithRGB(0xdbdbdb);
    
    self.oneLineView = oneLineView;
    
    [self.contentView addSubview:self.oneLineView];
    
    [self.oneLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.borrowMoneyValueLabel.mas_trailing).offset(17 * SCREENH6);
        make.centerY.mas_equalTo(self.borrowMoneyValueLabel);
        make.width.mas_equalTo(1 * SCREENH6);
        make.height.mas_equalTo(10 * SCREENH6);
    }];
    
    self.repayMoneyLabel = [self setupBigLabelWith:@"还款金额:"];
    
    [self.contentView addSubview:self.repayMoneyLabel];
    
    [self.repayMoneyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.oneLineView.mas_trailing).offset(14 * SCREENH6);
        make.centerY.mas_equalTo(self.oneLineView);
    }];
    
    self.repayMoneyValueLabel = [self setupBigLabelWith:@"￥100.00"];
    
    [self.contentView addSubview:self.repayMoneyValueLabel];
    
    [self.repayMoneyValueLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.repayMoneyLabel);
        make.leading.mas_equalTo(self.repayMoneyLabel.mas_trailing);
    }];
    
    UIImageView * stateImageView = [UIImageView new];
    
    stateImageView.image = [UIImage imageNamed:@"已还款"];
    
    self.stateImageView = stateImageView;
    
    [self.contentView addSubview:self.stateImageView];
    
    [self.stateImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo (self.backView);
        make.height.mas_equalTo(45 * SCREENH6);
        make.width.mas_equalTo(58 * SCREENH6);
        make.trailing.mas_equalTo(self.contentView).offset(-4 * SCREENH6);
    }];
    
    UIView * twoLineView = [UIView new];
    
    twoLineView.backgroundColor = ColorWithRGB(0xdbdbdb);
    
    self.twoLineView = twoLineView;
    
    [self.contentView addSubview:self.twoLineView];
    
    [self.twoLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.contentView).offset(6 * SCREENH6);
        make.trailing.mas_equalTo(self.contentView).offset(-6 * SCREENH6);
        make.height.mas_equalTo(1 * SCREENH6);
        make.top.mas_equalTo(self.contentView).offset(60 * SCREENH6);
    }];
    
    self.borrowTimeLabel = [self setupSmallLabelWith:@"借款日期"];
    
    [self.contentView addSubview:self.borrowTimeLabel];
    
    [self.borrowTimeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.twoLineView.mas_bottom).offset(23 * SCREENH6);
        make.leading.mas_equalTo(self.backView).offset(38 * SCREENH6);
    }];
    
    self.borrowTimeValueLabel = [self setupSmallLabelWith:@"2017-06-01"];
    
    [self.contentView addSubview:self.borrowTimeValueLabel];
    
    [self.borrowTimeValueLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.borrowTimeLabel);
        make.top.mas_equalTo(self.borrowTimeLabel.mas_bottom).offset(3 * SCREENH6);
    }];
    
    UIView * threeLineView = [UIView new];
    
    threeLineView.backgroundColor = ColorWithRGB(0xdbdbdb);
    
    self.threeLineView = threeLineView;
    
    [self.contentView addSubview:self.threeLineView];
    
    [self.threeLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.borrowTimeValueLabel.mas_trailing).offset(28 * SCREENH6);
        make.width.mas_equalTo(1 * SCREENH6);
        make.top.mas_equalTo(self.twoLineView.mas_bottom);
        make.bottom.mas_equalTo(self.contentView);
    }];
    
    self.repayTimeLabel = [self setupSmallLabelWith:@"还款日"];
    
    [self.contentView addSubview:self.repayTimeLabel];
    
    [self.repayTimeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.borrowTimeLabel);
        make.leading.mas_equalTo(self.threeLineView.mas_trailing).offset(45 * SCREENH6);
    }];
    
    self.repayTimeValueLabel = [self setupSmallLabelWith:@"2017-06-15"];
    
    [self.contentView addSubview:self.repayTimeValueLabel];
    
    [self.repayTimeValueLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.repayTimeLabel);
        make.centerY.mas_equalTo(self.borrowTimeValueLabel);
    }];
    
    
    UIView * fourLineView = [UIView new];
    
    fourLineView.backgroundColor = ColorWithRGB(0xdbdbdb);
    
    self.fourLineView = fourLineView;
    
    [self.contentView addSubview:self.fourLineView];
    
    [self.fourLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.repayTimeValueLabel.mas_trailing).offset(30 * SCREENH6);
        make.width.mas_equalTo(1 * SCREENH6);
        make.top.mas_equalTo(self.twoLineView.mas_bottom);
        make.bottom.mas_equalTo(self.contentView);
    }];
    
    self.payMoneyTimeLabel = [self setupSmallLabelWith:@"主动还款"];
    
    [self.contentView addSubview:self.payMoneyTimeLabel];
    
    [self.payMoneyTimeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.fourLineView.mas_trailing).offset(35 * SCREENH6);
        make.centerY.mas_equalTo(self.repayTimeLabel);
    }];
    
    self.payMoneyTimeValueLabel = [self setupSmallLabelWith:@"2017-06-02"];
    
    [self.contentView addSubview:self.payMoneyTimeValueLabel];
    
    [self.payMoneyTimeValueLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.payMoneyTimeLabel);
        make.centerY.mas_equalTo(self.repayTimeValueLabel);
    }];
}


- (UILabel *)setupBigLabelWith:(NSString *)string{
    
    UILabel * label = [UILabel new];
    
    label.font = [UIFont systemFontOfSize:15 * SCREENH6];
    label.textColor = ColorWithRGB(0x303c41);
    label.text = string;
    
    return label;
}


- (UILabel *)setupSmallLabelWith:(NSString *)string{
    
    UILabel * label = [UILabel new];
    
    label.font = [UIFont systemFontOfSize:12 * SCREENH6];
    label.text = string;
    label.textColor = ColorWithRGB(0x303c41);
    
    return label;
}



- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}



-(void)setModel:(LoanModel *)model
{
    _model = model;
//    model.loanTime;
    self.borrowMoneyValueLabel.text = [NSString stringWithFormat:@"%d元",[model.borrowAmount intValue]];
    self.repayMoneyValueLabel.text = [NSString stringWithFormat:@"%d元",[model.repayTotalAmount intValue]];
    self.repayMoneyValueLabel.text = [NSString stringWithFormat:@"%d元",[model.repayTotalAmount intValue]];
    self.borrowTimeValueLabel.text = [Lahelper formattedTimeFromTimeInterval:[model.applyTime longLongValue]];
    self.repayTimeValueLabel.text = [Lahelper formattedTimeFromTimeInterval:[model.paybackTime longLongValue]];
    self.payMoneyTimeValueLabel.text = [Lahelper formattedTimeFromTimeInterval:[model.paybackTime longLongValue]];
    switch ([model.unionStatus intValue]) {
            // 记录状态（1.审核中 2.审核未通过 3.审核打款中 4.待还款 5.已逾期 6.还款处理中 7.已还款 8.已逾期还款）
        case 1:
        {

            self.stateImageView.image = [UIImage imageNamed:@"审核中"];
        }
            break;
        case 2:
        {

            self.stateImageView.image = [UIImage imageNamed:@"未通过"];
            
        }
            break;
        case 3:
        {

            self.stateImageView.image = [UIImage imageNamed:@"打款中"];
            
        }
            break;
        case 4:
        {

            self.stateImageView.image = [UIImage imageNamed:@"还款中"];
            
        }
            break;
        case 5:
        {

            self.stateImageView.image = [UIImage imageNamed:@"逾期中"];
            
        }
            break;
        case 6:
        {

            self.stateImageView.image = [UIImage imageNamed:@"处理中"];
            
        }
            break;
        case 7:
        {

            self.stateImageView.image = [UIImage imageNamed:@"已还款"];
            
        }
            break;
        case 8:
        {

            self.stateImageView.image = [UIImage imageNamed:@"已逾期还款"];
            
            
        }
            break;
        default:
            break;
    }


}

@end
